<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\UserModel;
use app\portal\model\UserRankModel;
use cmf\controller\AdminBaseController;
use app\portal\model\UserOrganizeModel;
use app\portal\model\UserInfoModel;
use think\Db;

/**
 * Class AdminIndexController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'门户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'门户管理'
 * )
 */
class AdminIndexController extends AdminBaseController
{
    //移动首页
    public function index_mobile(){
        return $this->fetch();
    }
    //新闻统计
    public function news(){
        $user_model=new UserOrganizeModel();
        $data=$user_model->where('status',1)->column('org_id');
        $count= array_count_values($data);
        return json($count);
        //echo 1234;
    }
    //积分统计
    public function points(){
        $user_rank = new UserRankModel();
        $data=$user_rank->with('user')->order('all_points desc')->select();
        $user=[];$points=[];
        foreach ($data as $k=>$v){
            if($v['all_points']!=0){
                $user[]=$v['user']['user_nickname'];
                $points[]=$v['all_points'];
            }
        }
        $data=['user'=>$user,'points'=>$points];
        return json($data);

    }
    //活动人次统计
    public function activity(){

    }
    //用户统计
    public function user(){

    }
    //用户身份统计
//    public function work(){
//
//    }
    //用户年龄段统计
    public function age(){
        $user_model=new UserModel();
        $age[]=$user_model->where('birthday','<',strtotime('19800101'))->where('birthday','neq',0)->count();
        $age[]=$user_model->where('birthday','between',[strtotime('19800101'),strtotime('19900101')])->count();
        $age[]=$user_model->where('birthday','between',[strtotime('19900101'),strtotime('20000101')])->count();
        $age[]=$user_model->where('birthday','>',strtotime('20000101'))->where('birthday','neq',0)->count();
        return json($age);

    }
    public function car_info(){
        $user_id=input('user_id');
        $car_info=Db::name('user_info')->where('user_id',$user_id)->find();
        $this->assign('car_info',$car_info);
        $this->assign('user_id',$user_id);
        return $this->fetch();
    }
    public function  save_car_info(){
        $data=input('post.');
        unset($data['file']);
        $model=new UserInfoModel();
        $car_info=Db::name('user_info')->where('user_id',$data['user_id'])->find();
        if(!$car_info){
            $res=$model->insert($data);
        }else{
            $res=$model->where('user_id',$data['user_id'])->update($data);
        }

        if($res){
            $this->success('添加车辆信息成功！',url('/portal/admin_index/car_info',['user_id'=>$data['user_id']]));
        }else{
            $this->error('添加车辆信息失败！',url('/portal/admin_index/car_info',['user_id'=>$data['user_id']]));
        }
    }
    /**
     * 后台管理员退出
     */
    public function logout()
    {
        session('ADMIN_ID', null);
        return redirect(url('/admin', [], false, true));
    }



}
